﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
//using System.Runtime.Serialization.Json;

namespace AutoUpdater
{
    public class stockDataQuery
    {
        public float getCurrentPrice(string symbol)
        {
            float price = -999;
            //string format = "e1snl1"; // e1 = error indicator, s = symbol, n = company name, l1 = last trade price
            string format = "e1l1";
            Stream srcStream = queryStockDataSource(symbol, format);

            StreamReader read = new StreamReader(srcStream);
            
            //read the csv content
            string content = read.ReadToEnd();
                string[] dataElem = content.Split(',');
                if (!string.Equals(dataElem[0], "\"N/A\""))
                {
                    Console.Write("Unknown symbol!\n");
                }
                else
                {                   
                    price = float.Parse(dataElem[1]);
                }
            return price;
        }
        public int getShareOutStanding(String symbol)
        {
            Int32 shareOwned = -999;
            //string format = "e1snl1"; // e1 = error indicator, s = symbol, n = company name, l1 = last trade price
            string format = "e1v";
            Stream srcStream = queryStockDataSource(symbol, format);

            StreamReader read = new StreamReader(srcStream);

            //read the csv content
            string content = read.ReadToEnd();
            string[] dataElem = content.Split(',');
            if (!string.Equals(dataElem[0], "\"N/A\""))
            {
                Console.Write("Unknown symbol!\n");
            }
            else
            {
                shareOwned =Int32.Parse(dataElem[1]);
            }
            return shareOwned;
        }
        public string getCompanyName(string symbol)
        {
            string name = "NULL";
            //string format = "e1snl1"; // e1 = error indicator, s = symbol, n = company name, l1 = last trade price
            string format = "e1n";
            Stream srcStream = queryStockDataSource(symbol, format);

            StreamReader read = new StreamReader(srcStream);

            //read the csv content
            string content = read.ReadToEnd();
            string[] dataElem = content.Split(',');
            if (!string.Equals(dataElem[0], "\"N/A\""))
            {
                Console.Write("Unknown symbol!\n");
            }
            else
            {
                name = dataElem[1].Trim().TrimStart('"').TrimEnd('\n', '"'); //set name, and trim newline character if there
            }
            return name;
        }


       



        //Query CSV source directly, returns a stream to be parsed by the calling method
        private Stream queryStockDataSource(string symbol, string formatSTR)
        {
            string url = "http://finance.yahoo.com/d/quotes.csv?s=||SYM||&f=||FORMAT||";
            url = url.Replace("||SYM||", symbol);
            url = url.Replace("||FORMAT||", formatSTR);
            

            //Make http request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            WebResponse resp = req.GetResponse();

            //Get the response
            Stream receive = resp.GetResponseStream();
            //StreamReader read = new StreamReader(receive);

            //read the csv content
            //string content = read.ReadToEnd();
            
            //Console.Write(content);

            return (receive);


        }

        public Stock getStockInfo(string symbol)
        {
            //string format = "e1snl1"; // e1 = error indicator, s = symbol, n = company name, l1 = last trade price
            string format = "e1n";
            Stream srcStream = queryStockDataSource(symbol, format);

            StreamReader read = new StreamReader(srcStream);

            Stock stk = null;
            //read the csv content
            string content = read.ReadToEnd();
            string[] dataElem = content.Split(',');
            if (!string.Equals(dataElem[0], "\"N/A\""))
            {
                Console.Write("Unknown symbol!\n");
            }
            else
            {
                stk = new Stock(symbol);                
            }
            return stk;
        }
        



    }
}
